package com.bjkcst.baselib;

import android.app.Application;
import android.provider.Settings;

import com.bjkcst.baselib.utils.LogUtil;
import com.bjkcst.baselib.utils.Crashhandler;
import com.jakewharton.processphoenix.ProcessPhoenix;

import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author: glc.
 * Date: 2023/3/14  15:46
 * Description:
 */
public abstract class KcstApplication extends Application {

    private static Application sApplication;
    //删除日志周期
    private static final long DEFATE_LOG_CYCLE_START = 0;
    private static final long DELETE_LOG_CYCLE = 5;

    private final CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    @Override
    public void onCreate() {
        super.onCreate();
        if (ProcessPhoenix.isPhoenixProcess(getApplicationContext())){return;}
        Thread.setDefaultUncaughtExceptionHandler(new Crashhandler());
        sApplication = this;
        init();
        timingDeleteLocalLog();
    }

    public static Application getApplication(){
        return sApplication;
    }

    /**
     * 初始化
     */
    protected abstract void init();

    /**
     * 定时删除日志
     */
    private void timingDeleteLocalLog(){
        Disposable logSubscribe = Observable.interval(DEFATE_LOG_CYCLE_START,
                        DELETE_LOG_CYCLE, TimeUnit.DAYS, Schedulers.io())
                .subscribe(aLong -> {
                    LogUtil.delete();
                }, throwable -> {
                    mCompositeDisposable.dispose();
                    timingDeleteLocalLog();
                });
        mCompositeDisposable.add(logSubscribe);
    }
}
